package main

import (
	"fmt"
	"go_code/test/hello"
	"strconv"

	"github.com/gin-gonic/gin"
	"github.com/unrolled/secure"
)

type T struct {
	Path  string      `json:"path"`
	Value interface{} `json:"value"`
}

func LoadTls(port int) gin.HandlerFunc {
	return func(c *gin.Context) {
		middleware := secure.New(secure.Options{
			SSLRedirect: true,
			SSLHost:     "localhost:" + strconv.Itoa(port),
		})
		err := middleware.Process(c.Writer, c.Request)
		if err != nil {
			//如果出现错误，请不要继续。
			fmt.Println(err)
			return
		}
		// 继续往下处理
		c.Next()
	}
}

type ConfigParseServer struct {
}

func (*ConfigParseServer) GinHttps(isHttps bool) error {

	router := gin.Default()

	router.GET("/getConfigs/config", hello.GetAllConfigData)
	router.GET("/getConfig/serverVersion", hello.GetServerVersionInfo)
	router.GET("/getConfig/server", hello.GetServerInfo)
	router.GET("/getConfig/client", hello.GetClientInfo)
	router.GET("/getConfig/microConference", hello.GetMicroConferenceInfo)
	router.GET("/getConfig/callRecordServices", hello.GetCallRecordServicesInfo)
	router.GET("/getConfig/shareMeeting", hello.GetShareMeetingInfo)
	router.GET("/getConfig/conference", hello.GetConferenceInfo)
	router.GET("/getConfig/h5Server", hello.GetH5ServerInfo)
	router.GET("/getConfig/lvs", hello.GetLvsInfo)
	router.GET("/getConfig/extendServer", hello.GetExtendServerInfo)
	router.GET("/getConfig/syncServices", hello.GetSyncServicesInfo)
	router.GET("/getConfig/agent", hello.GetAgentInfo)
	router.GET("/getConfig/forceUpdate", hello.GetForceUpdateInfo)
	router.GET("/getConfig/ddioServer", hello.GetDdioServerInfo)
	router.GET("/getConfig/diversion", hello.GetDiversionInfo)
	router.GET("/getConfig/dataserver", hello.GetDataserverInfo)
	router.GET("/getConfig/versionMark", hello.GetVersionMarkInfo)
	router.GET("/getConfig/versionExplain", hello.GetVersionExplainInfo)
	router.GET("/getConfig/router", hello.GetRouterInfo)

	router.POST("/setServerVersionInfo/", hello.SetServerVersionInfo)
	router.POST("/setServerInfo/", hello.SetServerInfo)

	router.POST("/setClientInfo/", hello.SetClientInfo)
	router.POST("/setMicroConferenceInfo/", hello.SetMicroConferenceInfo)
	router.POST("/setCallRecordServicesInfo/", hello.SetCallRecordServicesInfo)
	router.POST("/setShareMeetingInfo/", hello.SetShareMeetingInfo)
	router.POST("/setConferenceInfo/", hello.SetConferenceInfo)
	router.POST("/setH5ServerInfo/", hello.SetH5ServerInfo)

	router.POST("/setLvsInfo/", hello.SetLvsInfo)
	router.POST("/setExtendServerInfo/", hello.SetExtendServerInfo)
	router.POST("/setSyncServicesInfo/", hello.SetSyncServicesInfo)
	router.POST("/setAgentInfo/", hello.SetAgentInfo)
	router.POST("/setForceUpdateInfo/", hello.SetForceUpdateInfo)

	router.POST("/setDdioServerInfo/", hello.SetDdioServerInfo)
	router.POST("/setDiversionInfo/", hello.SetDiversionInfo)
	router.POST("/setDataserverInfo/", hello.SetDataserverInfo)
	router.POST("/setVersionMarkInfo/", hello.SetVersionMarkInfo)
	router.POST("/setVersionExplainInfo/", hello.SetVersionExplainInfo)
	router.POST("/setRouterInfo/", hello.SetRouterInfo)

	if isHttps {
		router.Use(LoadTls(8000))
		return router.RunTLS(":8000", "server.crt", "server.key")
	}
	return router.Run(":8080")
}

func main() {
	var configParseServer ConfigParseServer
	fmt.Printf("请输入0/1来确定是否使用https: ")
	var valve bool
	fmt.Scan(&valve)
	configParseServer.GinHttps(valve)

}
